package com.lxk.controller;

import com.lxk.pojo.Appoint;
import com.lxk.pojo.Designer;
import com.lxk.pojo.Measure;
import com.lxk.service.BalconySevice;
import com.lxk.service.UtilService;
import com.lxk.vo.AppointMeasureModel;
import com.lxk.vo.AppointModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JohnSoybean
 * @create 2020/4/9 10:08
 * @description
 */
@Controller
public class BalconyController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BalconySevice bs;
    @Autowired
    private UtilService us;


    /**
     * 设计师列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/designer")
    public String toDesigner(HttpServletRequest req, Model model,
                            Integer pageIndex, Integer num, Integer totalPages,
                            Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 15;
        }
        if (null == totalPages) {
            totalPages = bs.getDesignerPages(num);
        }
        PageModel<Designer> designers = bs.selDesignerPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", designers);
        return "designer";
    }

    /**
     * 获取设计师信息
     * @param designerId
     * @return
     */
    @GetMapping("/designer/query")
    @ResponseBody
    public Result<Designer> getDesignerInfo(Integer designerId) {
        Designer designer = bs.selDesignerById(designerId);
        return Result.success(designer);
    }


    /**
     * 删除设计师
     * @param designerId
     * @return
     */
    @GetMapping("/designer/delete")
    @ResponseBody
    public Result<Integer> deleteDesigner(Integer designerId) {
        bs.deleteDesigner(designerId);
        return Result.success(1);
    }

    /**
     * 添加设计师
     * @param designer
     * @return
     */
    @PostMapping("/designer/add")
    @ResponseBody
    public Result<Integer> addDesigner(Designer designer, MultipartFile file) {
        if(!file.isEmpty()){
            String pic = us.uploadPicture(file);
            designer.setDesignerPic(pic);
        }else{
            return Result.error(CodeMsg.EMPTY_FILE);
        }
        bs.addDesigner(designer);
        return Result.success(1);
    }

    /**
     * 编辑设计师
     * @param designer
     * @return
     */
    @PostMapping("/designer/modify")
    @ResponseBody
    public Result<Integer> modifyDesigner(Designer designer, MultipartFile file) {
        if(!file.isEmpty()){
            String pic = us.uploadPicture(file);
            designer.setDesignerPic(pic);
        }
        log.info(designer.toString());
        bs.modifyDesigner(designer);
        return Result.success(1);
    }


    /**
     * 预约列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/appoint")
    public String toAppoint(HttpServletRequest req, Model model,
                             Integer pageIndex, Integer num, Integer totalPages,
                             Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = bs.getAppointPages(num);
        }
        PageModel<AppointModel> pages = bs.selAppointPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", pages);
        return "appoint";
    }

    /**
     * 获取预约信息
     * @param appointId
     * @return
     */
    @GetMapping("/appoint/query")
    @ResponseBody
    public Result<Appoint> getAppointInfo(Integer appointId) {
        Appoint appoint = bs.selAppointById(appointId);
        return Result.success(appoint);
    }

    /**
     * 预约设为已完成
     * @param appointId
     * @return
     */
    @GetMapping("/appoint/handle")
    @ResponseBody
    public Result<Integer> markAppointHandled(Integer appointId) {
        bs.handleAppoint(appointId);
        return Result.success(1);
    }


    /**
     * 上门量房 - 预约列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/measure")
    public String toMeasureAppoint(HttpServletRequest req, Model model,
                            Integer pageIndex, Integer num, Integer totalPages,
                            Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = bs.getMeasureAppointPages(num);
        }
        PageModel<AppointMeasureModel> pages = bs.selMeasureAppointPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", pages);
        return "measure";
    }

    /**
     * 获取预约信息
     * @param measureId
     * @return
     */
    @GetMapping("/measure/query")
    @ResponseBody
    public Result<Measure> getMeasureInfo(Integer measureId) {
        Measure measure = bs.selMeasureById(measureId);
        return Result.success(measure);
    }

    /**
     * 预约设为已完成
     * @param measureId
     * @return
     */
    @GetMapping("/measure/handle")
    @ResponseBody
    public Result<Integer> markMeasureHandled(Integer measureId) {
        bs.handleMeasure(measureId);
        return Result.success(1);
    }

}
